package com.cnd.scm.code.generator.controller;


import com.cnd.scm.code.generator.service.GeneratorService;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @version V1.0
 * @ClassName: GeneratorController
 * @Description:TODO()
 * @author: 李启联
 * @date: 2020-01-10 16:34
 * @Copyright: Copyright (c), 2020
 * 申明：本内容未经授权,禁止外泄以及用于其他的商业项目.
 */
@RestController
@RequestMapping("/generator")
public class GeneratorController {

    @Autowired
    private GeneratorService generatorService;
    /**
     * 生成代码
     */
    @RequestMapping("/code")
    public void code(String basePackage ,String tables, HttpServletResponse response) throws IOException {


        byte[] data = generatorService.generatorCode(basePackage,tables);

        response.reset();
        response.setHeader("Content-Disposition", "attachment; filename=\"generator.zip\"");
        //response.setHeader("Content-Disposition", "attachment; filename=\""+basePackage+".zip\"");
        response.addHeader("Content-Length", "" + data.length);
        response.setContentType("application/octet-stream; charset=UTF-8");

        IOUtils.write(data, response.getOutputStream());
    }
}